package hsb.ruoyi.common.converter;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author 胡帅博
 * @date 2023/1/5 0:01
 */
@Provider
public class LocalDateParamConverterProvider implements ParamConverterProvider {

//    @SuppressWarnings("unchecked")
//    @Override
//    public <T> ParamConverter<T> getConverter(Class<T> rawType,
//                                              Type genericType,
//                                              Annotation[] annotations) {
//        if (rawType.isAssignableFrom(LocalDateTime.class)) {
//            return (ParamConverter<T>) new LocalDateTimeParamConverter();
//        }
//        return null;
//    }

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
        if (rawType.isAssignableFrom(LocalDate.class)) {
            return (ParamConverter<T>) new LocalDateParamConverter();
        } else if (rawType.isAssignableFrom(LocalDateTime.class)) {
            return (ParamConverter<T>) new LocalDateTimeParamConverter();
        }
        return null;
    }
}
